package com.ivsign.android.IDCReader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.ivsign.android.IDCReader.IDCReaderSDK;
import com.siecom.tool.LogUtil;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class IDCReadInterface {
	final static String TAG = "IDCReadInterface";
    static {
        System.loadLibrary("idcard");
    }
    public static int DLL_IDOpen(){
        return DLL_IDOpen(0);
    }
    public static int DLL_IDOpen(int type){// 0 身份证 1 NFC
		LogUtil.e(TAG,"DLL_IDOpen Build.MODEL=" + Build.MODEL);

    	if(Build.MODEL.equals("A108Plus"))  gpio_control(100,0);
    	if(gpio_read(46)==false){
			LogUtil.e(TAG,"gpio control id card power up");
			gpio_control(46,1);
			int ms = 0;
			if(type == 0) ms = 1200;
			if(type == 1) ms = 10;
			try {
                Thread.sleep(ms);//
            } catch (InterruptedException e){
                e.printStackTrace();
            }
		}
    	return 0;
    }
    public static int DLL_IDClose(){
    	LogUtil.e(TAG,"---DLL_IDClose----");
    	gpio_control(46,0);
    	return 0;
    }
    public static byte[] DLL_IDReadCard(Bitmap[] bitmap){
    	int[] text_len = new int[1];
		byte[] text = new byte[3000];

		byte[] CardInfo = null;
		short result = 0;
		result = ReadCard(text_len,text);
		if(result==0){
			CardInfo = new byte[text_len[0]];
			System.arraycopy(text, 0, CardInfo, 0, text_len[0]);

			int ret = IDCReaderSDK.Init();
	        if (ret == 0)
	        {
	            byte[] datawlt = new byte[1384];
	            byte[] byLicData = {(byte)0x05,(byte)0x00,(byte)0x01,(byte)0x00,(byte)0x5B,(byte)0x03,(byte)0x33,(byte)0x01,(byte)0x5A,(byte)0xB3,(byte)0x1E,(byte)0x00};
	            for(int i = 14+256; i < 1295+2; i++)
	            {
	                datawlt[i] = text[i+2-10];
	            }
	            int t = IDCReaderSDK.unpack(datawlt,byLicData);
	            if(t != 1)
	            {
	                Log.e("cvr", "unpack  fail");
	                //DisplayInfo("IDCReaderSDK.unpack fail");

	            }
	            String facedefaultfile =Environment.getExternalStorageDirectory() + "/wltlib/zp.bmp";
	            File facefile = new File(facedefaultfile) ;

	            if(facefile.exists()) {
	                //Bitmap resize = Bitmap.createScaledBitmap(bmp, 102, 106, true);
	                //ivfaceimage.setImageBitmap(bmp);
					bitmap[0] = BitmapFactory.decodeFile(facedefaultfile);
	            }
	        }
	        else
	        {
	            Log.e("cvr","IDCReaderSDK.Init fail");
	         //   DisplayInfo("IDCReaderSDK.Init fail");
	        }
		}
    	return CardInfo;
    }
    public native static short ReadCard(int []text_len, byte []text);
	

    public static void gpio_control(int gpio, int mode){
		try
        {
			String file_name = new String("/sys/class/gpio/gpio"+gpio+"/value");
			File file = new File(file_name);
			if(file.exists()==false){
				file_name = new String("/sys/class/gpiocontrol/gpiocontrol/gpiocontrol"+gpio);
				file = new File(file_name);
			}
			
			FileWriter localFileWriter = new FileWriter(file);
		    if(mode==1)
		       localFileWriter.write("1");
		    else if(mode==0)
		       localFileWriter.write("0");
		    localFileWriter.close();
	    }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
	}
	private static boolean gpio_read(int gpio){
		try
        {
			String file_name = new String("/sys/class/gpio/gpio"+gpio+"/value");
			File file = new File(file_name);
			if(file.exists()==false){
				file_name = new String("/sys/class/gpiocontrol/gpiocontrol/gpiocontrol"+gpio);
				file = new File(file_name);
			}
			
			FileReader localFileRead = new FileReader(file);
			char[] ch = new char[1];
			localFileRead.read(ch);
			localFileRead.close();
			if(ch[0]=='1') return true;
			return false;
			
	    }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
		return false;
	}
}
